package package01.utils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

/**
 * 作者: jack
 * 时间: 2020-07-21 09:26
 * 描述: ObjectDecoder
 */
public class ObjectDecoder {

    public static <T> T byte2obj(byte[] buf) {
        // 将数组保存到字节数组流
        ByteArrayInputStream bi = new ByteArrayInputStream(buf);
        ObjectInputStream in = null;
        // 要返回的对象
        Object result = null;
        try {
            // 对象流到字节数组流中读取
            in = new ObjectInputStream(bi);
            result = in.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bi.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return (T) result;
    }

}
